package com.neu.utils;

/**
 * Created with IntelliJ IDEA.
 *
 * @author 17478
 * @ClassName: GlobalExceptionHandler
 * @description: TODO
 * @date 2024-11-15 19:37
 * @version: 1.0
 */
import com.neu.base.Result;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class MyGlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result<?> handleBusinessException(BusinessException ex) {
        // 使用 Result.error 方法返回统一格式的错误响应
        return Result.error(ex.getCode(), ex.getMessage());
    }

//    @ExceptionHandler(Exception.class)
//    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
//    public Result<?> handleException(Exception ex) {
//        // 其他未捕获的异常，返回通用的服务器错误
//        return Result.error(ErrorCode.INTERNAL_SERVER_ERROR);
//    }
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result<?> handleException(Exception ex) {
        // 使用 ErrorCode.getMessage() 获取错误的描述信息作为 String 类型
        return Result.error(ErrorCode.INTERNAL_SERVER_ERROR.getMessage());
}

}

